package com.atguigu.juc.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description: synchronized 和 reentrantLock  都是可重入锁
 * @author： yeswater
 * @create： 2024/5/29
 */

public class ReEntryLockDemo {
	
	public static void main(String[] args) {
		reEntry1();
		reEntry2();
	}
	
	private static void reEntry1() {
		Object o = new Object();
		new Thread(() -> {
			synchronized (o) {
				System.out.println("==== 1 ====");
				synchronized (o) {
					System.out.println("==== 2 ====");
					synchronized (o) {
						System.out.println("==== 3 ====");
					}
				}
			}
		}, "t1").start();
	}
	
	private static void reEntry2() {
		ReentrantLock lock = new ReentrantLock();
		new Thread(() -> {
			lock.lock();
			try {
				System.out.println("==== 1 ====");
				lock.lock();
				try {
					System.out.println("==== 2 ====");
					lock.lock();
					try {
						System.out.println("==== 3 ====");
					} finally {
						lock.unlock();
					}
				} finally {
					lock.unlock();
				}
			} finally {
				lock.unlock();
			}
		}, "t2").start();
	}
	
}
